package com.shiwaixiangcun.customer.module.heath

import android.graphics.Color
import android.graphics.Rect
import android.os.Build
import android.os.Bundle
import android.support.annotation.LayoutRes
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.ImageView
import butterknife.ButterKnife
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.shiwaixiangcun.customer.Common
import com.shiwaixiangcun.customer.R
import com.shiwaixiangcun.customer.base.BaseActivity
import com.shiwaixiangcun.customer.entity.ToolBean
import com.shiwaixiangcun.customer.ui.activity.LoginActivity
import com.shiwaixiangcun.customer.utils.DisplayUtil
import com.shiwaixiangcun.customer.utils.LoginHelper
import com.shiwaixiangcun.customer.utils.SharePreference
import kotlinx.android.synthetic.main.activity_health_evaluation.*
import kotlinx.android.synthetic.main.top_bar_transparent.*
import java.util.*

/**
 * Desc:    健康评测
 */
class HealthEvaluationActivity : BaseActivity(), View.OnClickListener {


    private var mGridAdapter: GridAdapter? = null
    private var mToolList: MutableList<ToolBean>? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_health_evaluation)
        if (Build.VERSION.SDK_INT >= 21) {
            val decorView = window.decorView
            val option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            decorView.systemUiVisibility = option
            window.statusBarColor = Color.TRANSPARENT
        }

        ButterKnife.bind(this)
        initData()
        initView()

    }


    private fun initData() {

        mToolList = ArrayList()
        val toolBean1 = ToolBean(1, "中医体质", R.drawable.icon_health_ctcm)
        val toolBean2 = ToolBean(2, "饮食生活习惯", R.drawable.icon_health_lifestyle)
        val toolBean3 = ToolBean(3, "内分泌系统", R.drawable.icon_health_endocrinium)
        val toolBean4 = ToolBean(4, "免疫系统", R.drawable.icon_health_immune)
        val toolBean5 = ToolBean(5, "消化系统", R.drawable.icon_health_digestive)
        val toolBean6 = ToolBean(6, "呼吸系统", R.drawable.icon_health_respiratory)
        val toolBean7 = ToolBean(7, "循环系统", R.drawable.icon_health_cyclic)
        val toolBean8 = ToolBean(8, "运动系统", R.drawable.icon_health_motor)
        val toolBean9 = ToolBean(9, "心理健康", R.drawable.icon_health_mental)
        val toolBean10 = ToolBean(10, "男性健康", R.drawable.icon_health_male)
        val toolBean11 = ToolBean(11, "女性健康", R.drawable.icon_health_female)
        mToolList!!.add(toolBean1)
        mToolList!!.add(toolBean2)
        mToolList!!.add(toolBean3)
        mToolList!!.add(toolBean4)
        mToolList!!.add(toolBean5)
        mToolList!!.add(toolBean6)
        mToolList!!.add(toolBean7)
        mToolList!!.add(toolBean8)
        mToolList!!.add(toolBean9)
        mToolList!!.add(toolBean10)
        mToolList!!.add(toolBean11)

    }

    private fun initView() {
        tv_page_name.text = "健康测评"

        mGridAdapter = mToolList?.let { GridAdapter(R.layout.layout_tool, it) }
        rv_grid.layoutManager = GridLayoutManager(mContext, 3)
        rv_grid.adapter = mGridAdapter
        back_left.setOnClickListener(this)
        mGridAdapter!!.setOnItemClickListener { adapter, view, position ->
            val bundle = Bundle()
            bundle.putParcelable("healthEvaluating", mToolList!![position])
            readyGo(CommonActivity::class.java, bundle)
        }

        rv_grid.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(outRect: Rect?, view: View?, parent: RecyclerView?, state: RecyclerView.State?) {
                outRect?.set(0, DisplayUtil.dip2px(mContext, 5f), 0, DisplayUtil.dip2px(mContext, 5f))
            }
        })

        tv_top_right.text = "历史测评"
        tv_top_right.setTextColor(Color.parseColor("#Ffffff"))
        tv_top_right.visibility = View.VISIBLE
        tv_top_right.setOnClickListener {
            val isLogin = SharePreference.getStringSpParams(this, Common.ISORNOLOGIN, Common.SIORNOLOGIN)


            if (LoginHelper.isLogin(this)) {
                readyGo(EvaluationRecordActivity::class.java)

            } else {
                readyGo(LoginActivity::class.java)
            }

        }

    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.back_left -> finish()
            else -> {
            }
        }
    }


    /**
     * Adapter
     */
    private inner class GridAdapter(@LayoutRes layoutResId: Int, toolList: List<ToolBean>) : BaseQuickAdapter<ToolBean, BaseViewHolder>(R.layout.layout_health, toolList) {

        override fun convert(helper: BaseViewHolder, item: ToolBean) {
            helper.setText(R.id.tv_tool_name, item.name)
            Glide.with(mContext).load(item.pic).into(helper.getView<View>(R.id.iv_tool) as ImageView)
        }

    }
}
